package com.woniuxy.yuekeuser.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yuekeuser.common.Result;
import com.woniuxy.yuekeuser.controller.Form.HotolCarForm;
import com.woniuxy.yuekeuser.model.City;
import com.woniuxy.yuekeuser.model.Hotolcar;
import com.woniuxy.yuekeuser.service.IHotolcarImageService;
import com.woniuxy.yuekeuser.service.IHotolcarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Collections;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@RestController
@Slf4j
@CrossOrigin("*")
public class HotolcarController {
    @Autowired
    private IHotolcarImageService iHotolcarImageService;
    @Autowired
    private IHotolcarService iHotolcarService;
    @GetMapping("getHotolCars")//分页查询全部房车  打乱一下 相当于默认排序
    public Result getHotolCars(HttpSession session,@RequestParam(defaultValue = "1")int currPage, @RequestParam(defaultValue = "3")int pageSize, @RequestParam(defaultValue = "0")int hotolcarPeopleNum, @RequestParam(defaultValue = "0")int choose){
        log.info("分页查询全部住宿房车的方法参数为{},{},{},{},{}",currPage,pageSize,hotolcarPeopleNum,choose);
        if(session.getAttribute("campsiteId")!=null){
            int campsiteId = (int)session.getAttribute("campsiteId");
            Page<HotolCarForm> page= new Page<>();
            page.setCurrent(currPage);
            page.setSize(pageSize);
            IPage<HotolCarForm> allHotolcar = iHotolcarService.findAllHotolcar(page, hotolcarPeopleNum,choose,campsiteId);
            for (int i=0;i<allHotolcar.getRecords().size();i++){
                System.out.println(allHotolcar.getRecords().get(i));
            }
            return Result.success(allHotolcar);
        }
       return Result.fail("当前营地不存在,没有房车");
    }

    @PostMapping("addHotolcarHeat")
    public Result addHotolcarHeat(Integer hotolcarId){
        log.info("执行增加房车热度的方法,房车的id为{}",hotolcarId);
        System.out.println(hotolcarId);
        if (hotolcarId!=null){
            Hotolcar hotolcar = iHotolcarService.queryHotolCarById(hotolcarId);
            hotolcar.setHotolcarHeat(hotolcar.getHotolcarHeat()+1);
            iHotolcarService.addHotolcarHeat(hotolcar);
            return Result.success();
        }
       return Result.fail("错误");

    }
}

